package com.lagou.homework02;

import java.util.Scanner;

public class Chess {
    String[][] board = new String[16][16];
    boolean flag = true;
    Scanner sc= new Scanner(System.in);
    String[] word = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
    public void begin(){
        for(int i = 0;i<16;i++){
            for(int j = 0;j<16;j++){
                board[i][j]="+";
            }
        }
    }
    public void chessBoard(){
        /*for(int i = 0;i<16;i++){
            for(int j = 0;j<16;j++){
                board[i][j]="+";
            }
        }*/
        System.out.print("\t");
        for(int i =0;i<word.length;i++) {
            System.out.print(word[i]+"\t");
        }
        for(int i =0;i<board.length;i++) {
            System.out.print("\n");
            System.out.print(word[i]+"\t");
            for(int j =0;j<word.length;j++){
                System.out.print(board[i][j]+"\t");
            }
        }
        System.out.println("\n");
    }
    public void play(){
        while(true) {
            if (flag) {
                System.out.println("请黑方下棋！");
                //System.out.println("棋子的横坐标为：");
                //System.out.println("棋子的纵坐标为：");
                flag = false;
            } else {
                System.out.println("请白方下棋！");
                //System.out.println("棋子的横坐标为：");
                //System.out.println("棋子的纵坐标为：");
                flag = true;
            }
            System.out.println("棋子的横坐标为：");
            int s1 = sc.nextInt();
            System.out.println("棋子的纵坐标为：");
            int s2 = sc.nextInt();
            if (flag == false) {
                board[s1][s2] = "0";//黑方为0
            } else {
                board[s1][s2] = "1";//白方为1
            }
            chessBoard();
            //判断是否胜利
            boolean a1=win(s1,s2);
            if(a1){
                System.out.println("恭喜获得胜利！");
                break;}
        }

    }
    boolean win(int x,int y){
        //判断横着连成5个没有
        //左边
        int count=0;
        for(int ix=x-1;ix>=0;ix--){
            if(board[ix][y]!=board[x][y]){
                break;
            }
            count++;
        }
        //右边
        for(int ix=x+1;ix<=15;ix++){
            if(board[ix][y]!=board[x][y]){
                break;
            }
            count++;
        }
        if(count==4){
            return true;
        }
        //判断竖着连成5个没有
        //上方
        count=0;
        for(int iy=y-1;iy>=0;iy--){
            if(board[x][iy]!=board[x][y]){
                break;
            }
            count++;
        }
        //下方
        for(int iy=y+1;iy<=15;iy++){
            if(board[x][iy]!=board[x][y]){
                break;
            }
            count++;
        }
        if(count==4){
            return true;
        }
        //判断斜着的有没有连成5个
        //左上到右下
        count = 0;
        for(int ix=x-1;ix>=0;ix--){
            int iy = y-1;
            if(board[ix][iy]!=board[x][y]){
                break;
            }
            count++;
        }
        for(int ix=x+1;ix<=15;ix++){
            int iy = ix;
            if(board[ix][iy]!=board[x][y]){
                break;
            }
            count++;
        }
        if(count==4){
            return true;
        }
        //右上到左下
        count = 0;
        //右上
        for(int ix=x+1,iy=y-1;ix<=15&&iy>=0;ix++,iy--){
            if(board[ix][iy]!=board[x][y]){
                break;
            }
            count++;
        }
        //左下
        for(int ix=x-1,iy=y+1;ix>=0&&iy<=15;ix--,iy++){
            if(board[ix][iy]!=board[x][y]){
                break;
            }
            count++;
        }
        if(count==4){return true;}
        return false;
    }

}
